<?php


namespace app\staff\controller;

use think\Db;
use addons\ttlock\ttlock;
use think\Config;


class Fangjian extends Base
{
    public function index()
    {
        $length = input('length');
        $list = Db::name('fangjian')->where('category_id', $this->shop_id)->limit($length, 30)->select();
        foreach ($list as &$item) {
            $item['image'] = cdnurl($item['image'], true);

        }
        return $this->success('获取成功', $list);
    }
    public function closing()
    {
        $ids = input("id");
        $data = Db::name('fangjian')->alias('b')
            ->field("b.id,b.dian_id")
            ->where("id", $ids)
            ->find();
        if (!$data) {
            return $this->error("房间不存在");
        }
        if (!$data['dian_id']) {
            return $this->error("房间关电命令不存在");
        }
        $res = Db::name("diancommand")->insert([
            'fangjian_id' => $data['id'],
            'status' => 2,
            'is_status' => 0,
            'create_time' => time(),
        ]);
        if (!$res) {
            return $this->error("写入失败");
        }
        return $this->success('等待服务器发送,耐心等待几秒钟');
    }

    public function opening()
    {
        $ids = input("id");
        $data = Db::name('fangjian')->alias('b')
            ->field("b.id,b.dian_id")
            ->where("id", $ids)
            ->find();
        if (!$data) {
            return $this->error("房间不存在");
        }
        if (!$data['dian_id']) {
            return $this->error("房间开电命令不存在");
        }
        $res = Db::name("diancommand")->insert([
            'fangjian_id' => $data['id'],
            'status' => 1,
            'is_status' => 0,
            'create_time' => time(),
        ]);
        if (!$res) {
            return $this->error("写入失败");
        }
        return $this->success('等待服务器发送,耐心等待几秒钟');
    }

    /**
     * 获取开锁密码
     */
    public function password()
    {
        $fj_id = input('fj_id');
        $detail = Db::name('fangjian')
            ->field("room_lock_id,category_id")
            ->where('id', $fj_id)
            ->find();
        if (!$detail) {
            $this->error('房间不存在');
        }
		$category_hall_lock_id = Db::name('category')->where('id', $detail['category_id'])->value('hall_lock_id');

        $ttlock = new ttlock();
        if ($category_hall_lock_id) {
            $hall_lock = $ttlock->getPassword($category_hall_lock_id, "hall_" . $fj_id);
            if ($hall_lock) {
                $hall_lock_id = $hall_lock;
            } else {
                $hall_lock_id = '密码获取失败';
            }
        } else {
            $hall_lock_id = '-';
        }

        if ($detail['room_lock_id']) {
            $room_lock = $ttlock->getPassword($detail['room_lock_id'], "room_" . $fj_id);
            if ($room_lock) {
                $room_lock_id = $room_lock;
            } else {
                $room_lock_id = '密码获取失败';
            }
        } else {
            $room_lock_id = '-';
        }
        return $this->success('查询成功', [
            'hall_lock_id' => $hall_lock_id,
            'room_lock_id' => $room_lock_id
        ]);

    }

    public function dasao()
    {
        $id = input('id');
        $state = input('state');
        Db::name('fangjian')
            ->where('id', $id)
            ->update([
                'is_dasao' => $state
            ]);
        $this->success('操作成功');
    }
}
